package org.bright.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.bright.dto.EmployeesDTO;
import org.bright.entity.Employees;
import org.bright.enums.HttpEnum;
import org.bright.exception.BusinessException;
import org.bright.mapper.EmployeesMapper;
import org.bright.service.IEmployeesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bright
 * @since 2024-10-21
 */
@Service
public class EmployeesServiceImpl extends ServiceImpl<EmployeesMapper, Employees> implements IEmployeesService {
    @Autowired
    private EmployeesMapper employeesMapper;
    @Override
    public PageInfo<Employees> selectAllByEmpNameAndPhoneNumber(int pageNum,String empName, String phoneNumber) {
        PageHelper.startPage(pageNum,20);
        List<Employees> employees = employeesMapper.selectAllByEmpNameAndPhoneNumber(empName, phoneNumber);
        PageInfo<Employees> pageInfo = new PageInfo<>(employees);
        return pageInfo;
    }

    @Override
    public int insertBatch(List<EmployeesDTO> employeesCollection) {
        return employeesMapper.insertBatch(employeesCollection);
    }

    @Override
    public int updateSelective(EmployeesDTO employeesDTO) {
        return employeesMapper.updateSelective(employeesDTO);
    }

    @Override
    public int deleteByEmployeeId(Integer employeeId) {
        return employeesMapper.deleteByEmployeeId(employeeId);
    }

    @Override
    public Employees selectAllByLoginNameAndPassword(String loginName, String password) {
//判断是否存在登录用户
        Employees employees = employeesMapper.selectAllByLoginName(loginName);
        if(employees==null){
             throw new BusinessException(HttpEnum.LOGIN_NAME_NOT_FOUND.getCode(),"用户不存在");
        }
        if(!employees.getPassword().equals(password)){
            throw new BusinessException(HttpEnum.PASSWORD_ERROR.getCode(),"密码错误");
        }
        return employees;
    }

    @Override
    public Employees selectAllByEmployeeId(Integer employeeId) {
        return employeesMapper.selectByEmployeeId(employeeId);
    }
}
